package com.fortune.service.impl;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.DSalesmanSettingDao;
import com.fortune.dto.in.dSalesmanSetting.DSalesmanSettingEditDto;
import com.fortune.dto.in.dSalesmanSetting.DSalesmanSettingListDto;
import com.fortune.dto.in.dUserMgt.DUserListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.service.DSalesmanSettingService;
import com.fortune.vo.dSalesmanSetting.DSalesmanSettingInfo;
import com.fortune.vo.dSalesmanSetting.ProNum;
import com.fortune.vo.dSalesmanSetting.User;
import com.fortune.vo.dSelect.DSelectProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/24 0024.
 */
@Service
public class DSalesmanSettingServiceImpl implements DSalesmanSettingService{
    @Autowired
    private DSalesmanSettingDao dSalesmanSettingDao;

    @Override
    public List<DSalesmanSettingInfo> findUser(String did) {
        return dSalesmanSettingDao.findUser(did);
    }

    @Override
    public List<User> findUsers(String did) {
        return dSalesmanSettingDao.findUsers(did);
    }

    @Override
    public PagingData<DSalesmanSettingInfo> findSalesmanSettingList(String did, String userId,DUserListDto dUserListDto) {
        List<DSalesmanSettingInfo> list = dSalesmanSettingDao.findJXSPros(did,dUserListDto);
       /* List<DSalesmanSettingInfo> lists = dSalesmanSettingDao.findZYPro(did,dUserListDto);*/
/*        for (DSalesmanSettingInfo dSalesmanSettingInfo : lists){
            dSalesmanSettingInfo.setMid("");
            dSalesmanSettingInfo.setmName("自营");
            list.add(dSalesmanSettingInfo);
        }*/
        for(DSalesmanSettingInfo one:list){
            DSalesmanSettingInfo aa= dSalesmanSettingDao.findSetting(one.getMid(),one.getMpid(),userId,did);
            if(aa == null){
                one.setMonthNum("0");
            }else{
                one.setMonthNum(aa.getMonthNum());
            }
        }
        List<DSalesmanSettingInfo> lists=dSalesmanSettingDao.count(did);
        int count = lists.size();
        PagingData<DSalesmanSettingInfo> pagingData=new PagingData<>(list,count);
        return pagingData;
    }

    @Override
    public List<ProNum> findSalesmanSettingLists(String did, String userId) {
        List<DSalesmanSettingInfo> list = dSalesmanSettingDao.findJXSPro(did);
        List<DSalesmanSettingInfo> lists = dSalesmanSettingDao.findZYPro(did);
        List<ProNum> list2 = new ArrayList<>();
        for (DSalesmanSettingInfo dSalesmanSettingInfo : lists){
            ProNum proNum = new ProNum();
            proNum.setMid("");
            proNum.setmName("自营");
            proNum.setMpid(dSalesmanSettingInfo.getMpid());
            proNum.setProName(dSalesmanSettingInfo.getProName());
            list2.add(proNum);
        }
        for(DSalesmanSettingInfo one:list){
            ProNum proNum = new ProNum();
            proNum.setMid(one.getMid());
            proNum.setmName(one.getmName());
            proNum.setMpid(one.getMpid());
            proNum.setProName(one.getProName());
            list2.add(proNum);
        }
        for(ProNum proNum:list2){
            DSalesmanSettingInfo aa= dSalesmanSettingDao.findSetting(proNum.getMid(),proNum.getMpid(),userId,did);
            if(aa == null){
                proNum.setMonthNum("0");
            }else{
                proNum.setMonthNum(aa.getMonthNum());
            }
        }
        return list2;
    }

    @Override
    public int editMonthNum(DSalesmanSettingEditDto editDto, String did) {
            String mid = editDto.getMid();
            String mpid = editDto.getMpid();
            String userId = editDto.getUserId();
               DSalesmanSettingInfo aa= dSalesmanSettingDao.findSetting(mid,mpid,userId,did);
               if(aa == null){
                   int num = dSalesmanSettingDao.addZYSalesmanSetting(mid,mpid,userId,did,editDto.getMonthNum());
                   if(num > 0){
                       return 1;
                   }else {
                       return 0;
                   }
               }else{
                   int num = dSalesmanSettingDao.editZYSalesmanSetting(mid,mpid,userId,did,editDto.getMonthNum());
                   if(num > 0){
                       return 1;
                   }else {
                       return 0;
                   }
               }
    }

    @Override
    public List<DSalesmanSettingInfo> findProduct(String did) {
        List<DSalesmanSettingInfo> list = dSalesmanSettingDao.findJXSPro(did);
        List<DSalesmanSettingInfo> lists = dSalesmanSettingDao.findZYPro(did);
        for (DSalesmanSettingInfo dSalesmanSettingInfo : lists){
            dSalesmanSettingInfo.setMid("");
            dSalesmanSettingInfo.setmName("自营");
            list.add(dSalesmanSettingInfo);
        }
        return list;
    }


    @Override
    public Float findUserNum(String mid, String mpid, String did, String userId,int day) {
        return dSalesmanSettingDao.findUserNum(mid,mpid,did,userId,day);
    }

}
